package com.shtel.paas.monitorcenter.util.commed;

import io.prometheus.client.Collector;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class CommandGaugeExports extends Collector{
	static List<Cmd> pss = new LinkedList<Cmd>();
	public List<Command> getCommand(List<Cmd> list){
		pss.clear();
		for (Cmd p : list) {
			pss.add(p);
		}
		//pss.add(new Process("10.145.247.140", "df -h"));
		SSHCommandExecutor.list.clear();
		new CommandGaugeExports().register();
		return SSHCommandExecutor.list;
	}
		

	@Override
	public List<MetricFamilySamples> collect() {
		// TODO Auto-generated method stub
		List<MetricFamilySamples> mfs = new ArrayList<MetricFamilySamples>();
		
		for(Cmd p:pss){
			try{
			SSHCommandExecutor sshExecutor = new SSHCommandExecutor(p.ip,p.command);
	        SSHCommandExecutor res = sshExecutor.execute();
	        
	        if(res!=null){
			List<String> vs = new ArrayList<String>();
			vs.add(res.ip);
			vs.add(res.command);
			vs.add(res.pid);
			vs.add(res.user);
			vs.add(res.cpu_use);
			vs.add(res.memory_use);
			vs.add(res.start_time);
			vs.add(res.cmd);
	        }else{
	        	System.err.println(p.ip+" "+p.command+" no cmd!");
	        }
			} catch (Exception e){
			  e.printStackTrace();
			  System.err.println(p.ip+"  connected refused!");
			}
		}
		
//		//SSHMultiCommandExecutor sshExecutor = new SSHMultiCommandExecutor("10.145.242.13","50070", "spark", "Spar1oon");
//		SSHMultiCommandExecutor sshExecutor = new SSHMultiCommandExecutor("10.7.82.22","10080", "patrol", "patrol-1");
//        
//		//根据端口取 pid       
//        SSHMultiCommandExecutor res;
//		try {
//			res = sshExecutor.execute();
//			List<String> ls = new ArrayList<String>();
//	        ls.add("ip");
//	        ls.add("port");
//	        ls.add("pid");
//	        ls.add("usr");
//	        ls.add("cpu");
//	        ls.add("mem");
//			ls.add("start");
//			ls.add("cmd");
//			GaugeMetricFamily cpu_used = new GaugeMetricFamily(
//			        "process_union",
//			        "some informations of specified process.",
//			         ls );
//			List<String> vs = new ArrayList<String>();
//			vs.add(res.ip);
//			vs.add(res.port);
//			vs.add(res.pid);
//			vs.add(res.user);
//			vs.add(res.cpu_use);
//			vs.add(res.memory_use);
//			vs.add(res.start_time);
//			vs.add(res.cmd);
//			cpu_used.addMetric(vs, 100000);
//			
//			mfs.add(cpu_used);
//		} catch (JSchException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
        
        
		
		
		return mfs;
	}

}
